package com.pingao.mp.modular.fls.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pingao.mp.config.dict.DictConfig;
import com.pingao.mp.modular.fls.entity.Activity;
import com.pingao.mp.modular.fls.entity.ActivityAwards;
import com.pingao.mp.modular.fls.service.ActivityAwardsService;
import com.pingao.mp.modular.fls.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 抽奖 前端控制器
 * </p>
 *
 * @author 闻品高
 * @since 2020-01-16
 */
@Controller
@RequestMapping("/fls/lottery")
public class LotteryController {
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityAwardsService activityAwardsService;

    @Autowired
    private DictConfig dictConfig;

    @GetMapping("/index")
    public String index(ModelMap modelMap){
        modelMap.addAttribute("genders",dictConfig.getGenders());
        return "fls/lottery/index";
    }

    @GetMapping("/get/{activityId}")
    @ResponseBody
    public Object getActivity(@PathVariable("activityId") Long activityId) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Activity activity = activityService.getById(activityId);
        if(activity == null){
            throw new Exception("活动不存在");
        }
        List<ActivityAwards> activityAwards = activityAwardsService.list(new QueryWrapper<ActivityAwards>().eq(ActivityAwards.COL_ACTIVITY_ID, activity.getId()));
        if(activityAwards.size() < 1){
            throw new Exception("该活动没有奖项");
        }
        map.put("activity",activity);
        map.put("activityAwards",activityAwards);
        map.put("status",true);
        map.put("msg","获取活动成功");
        return map;
    }
    @GetMapping("/lottery/{activityId}")
    @ResponseBody
    public Object lottery(@PathVariable("activityId") Long activityId) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Activity activity = activityService.getById(activityId);
        if(activity == null){
            throw new Exception("活动不存在");
        }
        List<ActivityAwards> activityAwards = activityAwardsService.list(new QueryWrapper<ActivityAwards>().eq(ActivityAwards.COL_ACTIVITY_ID, activity.getId()));
        if(activityAwards.size() < 1){
            throw new Exception("该活动没有奖项");
        }

        List<Long> ids = new ArrayList<>();
        for (ActivityAwards activityAward : activityAwards) {
            for (int i = 0; i < activityAward.getProbability() * 100; i++) {
                ids.add(activityAward.getId());
            }
        }
        int max = ids.size(), min = 0;
        long randomNum = System.currentTimeMillis();
        int r = (int) (randomNum % (max - min) + min);
        long id = ids.get(r);
        ActivityAwards awards = activityAwardsService.getById(id);
        map.put("activityAwards",awards);
        map.put("status",true);
        map.put("msg","抽奖成功");
        return map;
    }
}
